/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import oracle.jdbc.OracleTypes;
import queenb.net.datacontrol.DataStatement;
import queenb.net.datacontrol.Param;
import queenb.net.entitybean.SimpleObject;
import queenb.web.exception.NoDataEffectException;
import queenb.web.util.PackageContants;
import queenb.web.util.Util;

/**
 * @author  : linhlv
 * @package : queenb.web.store
 * @file    : CombCurpks.java
 * @created : Oct 4, 2010
 * @purpose : lay cac danh sach trong ComBCurPks package
 */
public class CombCurpks extends SimpleObject {

    private String procedureName = null;

    public CombCurpks() {
    }

    public CombCurpks(String procedureName) {
        this.procedureName=procedureName;
    }



    public List<SimpleObject> listSimpleObject(Param p[]) throws NoDataEffectException, SQLException, InstantiationException, IllegalAccessException {
        DataStatement dataStatement = null;
        List<SimpleObject> l = null;

        if (getProcedureName() != null) {
            dataStatement= new DataStatement(getProcedureName(), OracleTypes.CURSOR, p);
            System.out.println(getStringParameterForDebug(p));
            l = super.find(dataStatement, SimpleObject.class);
//            if (l == null || l.isEmpty()) {
//                throw new NoDataEffectException(getProcedureName() + ": no data with parameters: " + getStringParameterForDebug(p));
//            }
        }else{
                throw new NoDataEffectException("You must input your package name by using setProcedureName method");
        }
        return l;
    }

    public List<SelectItem> getListSelectItems(Param p[]) throws NoDataEffectException, SQLException, InstantiationException, IllegalAccessException{
        List<SimpleObject> lstSim = listSimpleObject(p);
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        for (int i = 0; i < lstSim.size(); i++) {
            SimpleObject simObj = lstSim.get(i);
            selectItems.add(new SelectItem(Util.parseString(simObj.getVal()), simObj.getLabel()));
        }
        return selectItems;
    }

    public List<SelectItem> getListSelectItemsVal2(Param p[]) throws NoDataEffectException, SQLException, InstantiationException, IllegalAccessException{
        List<SimpleObject> lstSim = listSimpleObject(p);
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        for (int i = 0; i < lstSim.size(); i++) {
            SimpleObject simObj = lstSim.get(i);
            selectItems.add(new SelectItem(Util.parseString(simObj.getVal2()), simObj.getLabel()));
        }
        return selectItems;
    }
    
    private String getStringParameterForDebug(Param[] p) {
        String strParam = "";
        for (Param a : p) {
            strParam += "TYPE: " + a.getType() + " VALUE:" + a.getValue().toString();
        }
        return strParam;
    }

    /**
     * @return the procedureName
     */
    public String getProcedureName() {
        return procedureName;
    }

    /**
     * @param procedureName the procedureName to set
     */
    public void setProcedureName(String procedureName) {
        this.procedureName = procedureName;
    }
}
